package com.lori.netshop.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lori.netshop.pojo.TbContent;
import com.lori.netshop.service.ContentService;
import com.lori.netshop.util.NetshopResult;

@Controller
@RequestMapping("/content")
public class ContentController {
	//依赖注入service
	@Autowired
	private ContentService contentServiceimp;
	/**
	 * 功能：根据id或全部获得tb_content表的数据
	 * @param id 商品的分类id
	 * @param page 页数
	 * @param rows 每页显示多少条数据
	 * @return 返回json格式的数据
	 * @throws JsonProcessingException
	 */
	@RequestMapping("/query/list")
	@ResponseBody
	public String queryContent(@RequestParam(value="categoryId",defaultValue="0") long id,Integer page,Integer rows) throws JsonProcessingException{
		Map queryContent = contentServiceimp.queryContent(page, rows, id);
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(queryContent);
		return json;
	}
	/**
	 * 功能：新增商品内容信息
	 * @param tbcontent
	 * @return
	 * @throws JsonProcessingException
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(TbContent tbcontent) throws JsonProcessingException{
		NetshopResult result = contentServiceimp.save(tbcontent);
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(result);
		return json;
	}
	/**
	 * 
	 * 功能：根据id更新数据
	 * @param tbcontent
	 * @return
	 * @throws JsonProcessingException
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public String edit(TbContent tbcontent) throws JsonProcessingException{
		NetshopResult result = contentServiceimp.edit(tbcontent);
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(result);
		return json;
	}
	@RequestMapping("/delete")
	@ResponseBody
	public NetshopResult delete(long[] ids){
		NetshopResult result = contentServiceimp.delete(ids);
		return result;
	}
	
}
